<?php
namespace app\admin\controller;

use think\annotation\route\Group;
use think\annotation\Route;
use think\annotation\route\Middleware;
use app\admin\middleware\CheckAdmin;
use think\facade\Db;

/**
 * 变更记录
 * Class ChangeLog
 * @package app\admin\controller
 * @Group("ChangeLog")
 *
 */
class ChangeLog extends Base
{
    /**
     * 变更记录列表
     * @Route("index", method="POST")
     * @middleware({CheckAdmin::class})
     */
    public function index()
    {
        $id = input('id', '', 'trim');
        $page = input('page', '', 'trim');
        $pagesize = input('psize', '', 'trim');
        $res = Db::name('change_log')
            ->join('record', 'record.id=change_log.project_record_id')
            ->where('change_log.project_record_id',$id)
            ->order('change_log.id desc')
            ->page($page, $pagesize)
            ->select()->all();
        $result['total'] = Db::name('middle_records')->where('project_record_id',$id)->count();
        $result['data'] = $res;
        return json_ok($result);
    }

    /**
     * 变更记录保存
     * @Route("save", method="POST")
     * @middleware({CheckAdmin::class})
     */
    public function save()
    {
        $id = input('id', '', 'trim');
        $type = input('type', '', 'trim');
        $apply_time = input('apply_time', '', 'trim');
        $content = input('content', '', 'trim');
        $material_id = input('material_id', '', 'trim');
        $data = [
            'project_record_id' => $id,
            'type' => $type,
            'apply_time' => strtotime($apply_time),
            'content' => $content,
            'material_id' => $material_id,
        ];
        Db::name('change_log')->insert($data);
        return json_ok([], 10000, '操作成功');
    }

    /**
     * 变更记录详情
     * @Route("getChangeLogById", method="POST")
     * @middleware({CheckAdmin::class})
     */
    public function getChangeLogById()
    {
        $id = input('id', '', 'trim');
        $res = Db::name('change_log')
            ->where('id',$id)
            ->find();
        return json_ok($res);
    }
}